// SCENARIO SCRIPT

// This is the special script for your entire scenario. It contains
// special encounters and code accessable from anywhere in the scenario. it also
// contains the code that initializes important special things in the
// scenario (like shops and names and descriptions of special items).

// You can create your own states, but you should give all of them numbers greater than
// or equal to 10.

beginscenarioscript;

variables;
	short left,coins,day,last_day,night_time;
	string messagestring;
body;

// This is the state that is called every time the scenario is loaded,
// even when a save file in the scenario is loaded. Some things that should go here:
//    Names and descriptions of special items.
//    Names and effects of custom special abilities.
beginstate LOAD_SCEN_STATE;
	break;

// This is the state that is called only once at the very beginning of 
// the scenario. Some things that should go here:
//    The stuff in shops.
//    Creating horses and boats.
beginstate START_SCEN_STATE;
	// It is night time. 0-500,4500-5000=night, 0=start
	set_ticks_forward(4500);


	// Make a horse! It's owned by the player and right next to them.
	create_horse(0,0,18,5,0);
	
	// Father Kernighan's horse, right in front of its carriage.
	create_horse(1,0,15,9,1);

	// Initialize a few shops.
	
	// Shop 0 - armor
	add_item_to_shop(0,25,25);
	add_item_to_shop(0,26,20);
	add_item_to_shop(0,30,20);
	add_item_to_shop(0,31,10);
	add_item_to_shop(0,35,10);
	add_item_to_shop(0,121,15);
	add_item_to_shop(0,122,15);
	add_item_to_shop(0,126,3);
	add_item_to_shop(0,131,2);
	add_item_to_shop(0,111,15);
	add_item_to_shop(0,16,10);
	add_item_to_shop(0,136,10);
	add_item_to_shop(0,137,10);
	add_item_to_shop(0,141,5);
	
	// Shop 1 - weapons
	add_item_to_shop(1,45,25);
	add_item_to_shop(1,46,20);
	add_item_to_shop(1,50,15);
	add_item_to_shop(1,51,10);
	add_item_to_shop(1,55,5);
	add_item_to_shop(1,65,20);
	add_item_to_shop(1,66,10);
	add_item_to_shop(1,70,5);
	
	// Shop 2 - tools
	add_item_to_shop(2,170,500);
	add_item_to_shop(2,171,500);
	add_item_to_shop(2,172,30);
	add_item_to_shop(2,174,500);
	add_item_to_shop(2,177,500);
	
	// Shop 3 - missiles
	add_item_to_shop(3,85,500);
	add_item_to_shop(3,86,10);
	add_item_to_shop(3,90,10);
	add_item_to_shop(3,95,3);
	add_item_to_shop(3,100,20);
	add_item_to_shop(3,101,10);
	add_item_to_shop(3,105,10);

	// Shop 4 - potions
	add_item_to_shop(4,220,4);
	add_item_to_shop(4,221,3);
	add_item_to_shop(4,222,3);
	add_item_to_shop(4,223,2);

	// Shop 5 - food
	add_item_to_shop(5,4,500);
	add_item_to_shop(5,6,500);
	add_item_to_shop(5,8,500);

	// Shop 6 - Kernighan's Rituals
	add_item_to_shop(6,3000,6); // heal
	add_item_to_shop(6,3001,5); // cure
	add_item_to_shop(6,3002,4); // bless
	add_item_to_shop(6,3003,3); // terror
	add_item_to_shop(6,3004,2); // repel spirit
	add_item_to_shop(6,3005,1); // smite

	// Shop 7 - Hopper's Archery Supplies
	add_item_to_shop(7,101,500); // iron arrows
	add_item_to_shop(7,102,500); // steel
	add_item_to_shop(7,106,500); // iron bolts
	add_item_to_shop(7,107,500); // steel
	add_item_to_shop(7,103,10);  // blessed (limited)
	add_item_to_shop(7,108,10);

	// quests
	init_quest(0,"Express Delivery","Your message must be delivered to the mayor of Lorelei within five days.");
	init_quest(1,"Get it Back","The message has been stolen. You must recover it or face all the Empire has in store for incompetent couriers.");
	toggle_quest(0,1);

	// special items
	init_special_item(0,"Courier Pouch","The leather bag is embossed with a golden crown and the silver wings standing for the Imperial postal service. It appears to be magically sealed, and also enchanted against destruction.");
	init_special_item(1,"Brass Key","This key was lying at the bottom of a chest in a hidden side passage in the goblin lair. Chances are that you will find a door it opens later.");
	init_special_item(2,"Courier Pouch","You retrieved this bag from a very spooky cave below the goblin lair. Hell if you know how it got there, but you're glad you have it back.");
	change_spec_item(0,1);
	
break;

// This state is called every tick wherever the party is in the scenario.
// You can use the set_state
beginstate START_STATE;
	day=what_day_of_scenario();
	last_day=get_flag(200,0);
	if (day>last_day) 
	{
		set_state_continue(10); // check the day counter
	}
	night_time=get_flag(200,2);
	// 0-500,4500-5000=night, 0=start
	if (get_current_tick()%5000<500 || get_current_tick()%5000>4500)
	{
		//print_str("Night falls.");
		night_time=1;
	} 
	else
	{
		//print_str("Day breaks.");
		night_time=0;
	}
	set_flag(200,2,night_time);
break;

// Place your own states below. Give each a number at least 10.
beginstate 10;
	left=5-day;
	coins=1000+500*left;
	set_flag(200,0,day);
	clear_buffer();
	append_string("A new day is dawning. You have ");
	if (left>0)
	{
		append_number(left);
		append_string(" day");
		if (left>1) append_string("s");
		append_string(" left to deliver");
		get_buffer_text(messagestring);
		print_str_color(messagestring,3);
		clear_buffer();
		append_string("your message, and your pay would currently be ");
		append_number(coins);
		append_string(" gold pieces.");
		get_buffer_text(messagestring);
		print_str_color(messagestring,3);
	} else if (left==0)
	{
		append_string("to deliver your message");
		get_buffer_text(messagestring);
		print_str_color(messagestring,1);
		print_str_color("before sundown, or your mission will be forfeit.",1);
		
	} else 
	{
		message_dialog("The sun has turned an angry, crimson red as it sets behind the hills to the west. The final day of your delivery deadline is ending, and you are nowhere near Lorelei.",
				"You have failed your mission. Whether you deliver your message now or not, you will face trouble, and this is the last Imperial mission you will have for long time.");
		end_scenario(0);
	}
	set_state_continue(2);
break;